# 你有一个目录，放了你一个月的日记，都是 txt，为了避免分词的问题，假设内容都是英文，请统计出你认为每篇日记最重要的词。

def add_tupe(dictory_path):
    import re
    import os
    '''将文件中的内容取出空格和换行，每个单词存入到tupe中'''
    words = {}
    pathDir =  os.listdir(dictory_path)
    for allDir in pathDir:
        child = os.path.join(dictory_path,allDir)
        print(child)

        # print child.decode('gbk') # .decode('gbk')是解决中文显示乱码问题
        file = open(child,'r',encoding='utf-8')
        str1 = file.read()
        reObj = re.compile('\b?(\w+)\b?')
        words[allDir] = reObj.findall(str1)   #将文章去除空格和换行，把单词存在字典中
        file.close()

    return words

def count_tupe(file_tupe):
    '''把列表中单词依次取出来，放在字典中。单词为key，次数为value。每出现一次，value+1'''
    # print(file_tupe.keys())
    wordDict = dict()
    new_wordDict = dict()
    for i in file_tupe.keys():
        print(i)
        for word in file_tupe[i]:
            # print('------',file_tupe[i])
            if word in wordDict:
                wordDict[word] += 1
                new_wordDict[i] =wordDict
            else:
                wordDict[word] = 1
                new_wordDict[i] = wordDict

    print(new_wordDict.keys(),new_wordDict)

count_tupe(add_tupe('e:\py\练习\练习'))
